package com.yangit.temp.util.pdf.test;

import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;

public class PDFTempDemo {
	/**
	 * 抠模板
	 * 
	 * @throws Exception
	 */
	public void createAllPdf() throws Exception {
		// 填充创建pdf
		PdfReader reader = null;
		PdfStamper stamp = null;
		try {
			reader = new PdfReader("E:/module.pdf");
			SimpleDateFormat simp = new SimpleDateFormat("yyyy-MM-dd");
			String times = simp.format(new Date()).trim();
			// 创建生成报告名称
			String root = "/upload" + File.separator;
			if (!new File(root).exists())
				new File(root).mkdirs();
			File deskFile = new File(root, times + ".pdf");
			stamp = new PdfStamper(reader, new FileOutputStream(deskFile));
			// 取出报表模板中的所有字段
			AcroFields form = stamp.getAcroFields();
			// 填充数据
			form.setField("name", "zhangsan");
			form.setField("sex", "男");
			form.setField("age", "15");

			// 报告生成日期
			SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd");
			String generationdate = dateformat.format(new Date());
			form.setField("generationdate", generationdate);
			stamp.setFormFlattening(true);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (stamp != null) {
				stamp.close();
			}
			if (reader != null) {
				reader.close();
			}
		}
	}
}
